//! @Author: DengLibin
//! @Date: Create in 2023-10-24 15:49:55
//! @Description:  文件夹
//!

use async_walkdir::{Filtering, WalkDir};
use futures_util::StreamExt;

/// @Author: DengLibin
/// @Date: Create in 2023-10-24 16:10:02
/// @Description: 获取文件夹下所有子文件(包括子级，不包括 .开头的文件)
pub async fn children_files(dir: &str) -> Vec<String> {
    //不要 .开头的文件
    let mut walkdir = WalkDir::new(dir).filter(|entry| async move {
        if let Some(true) = entry
            .path()
            .file_name()
            .map(|f| f.to_string_lossy().starts_with("."))
        {
            return Filtering::Ignore;
        }
        return Filtering::Continue;
    });
    let mut list = vec![];
    loop {
        match walkdir.next().await {
            Some(r) => match r {
                Ok(entry) => {
                    let path = entry.path();
                    let op = path.to_str();
                    if let Some(p) = op {
                        let mr = std::fs::metadata(p);
                        if let Ok(m) = mr {
                            if m.is_file() { //文件
                                list.push(String::from(p));
                            }
                        }
                    }
                }
                Err(_) => {}
            },
            None => {
                break;
            }
        }
    }

    return list;
}
